// 线段切分
import { _k, _polar, _rotate } from '../math/index.js'

// 圆弧切分 
export const polarPoints = (options = {}) => {

    let {
        o = [0, 0],
        r = 100,
        n = 6,
        a = 0,
        a2,
        direction = 1
    } = options

    let a1 = a
    a2 = a2 || (360 + a1)

    let ps = Array.from({
        length: n
    }, (_, i) => {
        let a = a1 + i * (a2 - a1) / n
        return _polar(o, r, a)
    })
    return direction === 1 ? ps : ps.reverse()
}

// 坐标网格线
export const polarGridPoints = (options) => {
    let {
        o = [0, 0],
        r = 100,
        n = 6,
        a = 0,
        m = 5
    } = options
    let a1 = a, a2 = 360 + a1
    return Array.from({ length: m }, (_, j) => {
        return Array.from({
            length: n
        }, (_, i) => {
            let a = a1 + i * (a2 - a1) / n
            return _polar(o, r * (j + 1), a)
        })
    })
}

// 等角
export const isometricPoints = (options = {}) => {
    let {
        o = [0, 0],
        r = 100,
        a = 45,
        n = 10
    } = options

    return Array.from({
        length: n
    }, (_, i) => {
        return _polar(o, r, a * i)
    })
}


// 根据线段 作图 多边形
// 逆时针
export const segToPolygonPoints = ([from, to], n, sweepFlag = true) => {

    // 内角
    let ia = 180 - 360 / n
    let points = [from, to]
    let fn = (p, o) => {
        let next = _rotate(p, o, ia * (sweepFlag ? 1 : -1)).map(t => _k(t))
        points.push(next)
        let len = points.length
        if (len < n) {
            fn(o, next)
        }
    }

    fn(from, to)
    return points
}



// 等边 已知边，作多边形
export const polygonPoints = (options = {}) => {
    let {
        o = [0, 0],
        r = 100,
        a = 45,
        n = 10,
        to,
        sweepFlag = true
    } = options

    let from = o
    to = to || _polar(o, r, a)

    return segToPolygonPoints([from, to], n, sweepFlag)

}
